﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TheMovieDB;
using System.Net;
using System.Drawing.Drawing2D;
using System.IO;

namespace tmdbClient
{
    public partial class Form1 : Form
    {
        List<Image> imgList = new List<Image>();
        TmdbMovie[] movies;
        TmdbAPI tmdb = new TmdbAPI("23a30e31f9e1b0a2394b43c32928e408");

        List<MultiMovie> multiResults = new List<MultiMovie>();

        List<MdbMovie> movs = new List<MdbMovie>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (string dir in Directory.GetDirectories(@"C:\movs"))
            {
                this.movs.Add(new MdbMovie(dir, tmdb));
            }

            foreach (MdbMovie movie in this.movs)
            {
                if (!movie.HasMovieXml)
                {
                    movie.GetMovieInfos();
                }
            }

            //foreach(string dir in Directory.GetDirectories(@"C:\movs"))
            //{
            //    TmdbMovie[] movs = tmdb.MovieSearch(dir.Substring(dir.LastIndexOf(@"\") + 1));
            //    int widthP = 0, widthB = 0;

            //    if (movs.Length == 1)
            //    {
            //        foreach (TmdbImage img in movs[0].Images)
            //        {
            //            if (img.Type == TmdbImageType.poster)
            //            {
            //                if (img.Width > widthP)
            //                {
            //                    WebRequest requestPic = WebRequest.Create(img.Url);
            //                    WebResponse responsePic = requestPic.GetResponse();
            //                    Image imgTemp = Image.FromStream(responsePic.GetResponseStream());

            //                    imgTemp.Save(dir + @"\folder.jpg");
            //                    widthP = img.Width;
            //                }
            //            }

            //            if (img.Type == TmdbImageType.backdrop)
            //            {
            //                if (img.Width > widthB)
            //                {
            //                    WebRequest requestPic = WebRequest.Create(img.Url);
            //                    WebResponse responsePic = requestPic.GetResponse();
            //                    Image imgTemp = Image.FromStream(responsePic.GetResponseStream());

            //                    imgTemp.Save(dir + @"\fanart.jpg");
            //                    widthB = img.Width;
            //                }
            //            }
            //        }
            //    }
            //    else
            //    {
            //        this.multiResults.Add(new MultiMovie(dir.Substring(dir.LastIndexOf(@"\")+1), movs, dir));
            //    }
            //}
            //foreach (MultiMovie movie in this.multiResults)
            //{
            //    ResultSelectForm frm = new ResultSelectForm(movie);
            //    frm.MovieSelected += new EventHandler(frm_MovieSelected);
            //    frm.ShowDialog();
            //}


            //this.lblStatus.Text = "fertig";
            //tmdb.MovieSearchCompleted += new TmdbAPI.MovieSearchAsyncCompletedEventHandler(this.movInfoComplete);
            
        }

        void frm_MovieSelected(object sender, EventArgs e)
        {
            int widthP = 0, widthB = 0;
            MultiMovie movie = ((MultiMovie)sender);
            foreach (TmdbImage img in movie.results[movie.selected].Images)
            {
                if (img.Type == TmdbImageType.poster)
                {
                    if (img.Width > widthP)
                    {
                        WebRequest requestPic = WebRequest.Create(img.Url);
                        WebResponse responsePic = requestPic.GetResponse();
                        Image imgTemp = Image.FromStream(responsePic.GetResponseStream());

                        imgTemp.Save(movie.dir + @"\folder.jpg");
                        widthP = img.Width;
                    }
                }

                if (img.Type == TmdbImageType.backdrop)
                {
                    if (img.Width > widthB)
                    {
                        WebRequest requestPic = WebRequest.Create(img.Url);
                        WebResponse responsePic = requestPic.GetResponse();
                        Image imgTemp = Image.FromStream(responsePic.GetResponseStream());

                        imgTemp.Save(movie.dir + @"\fanart.jpg");
                        widthB = img.Width;
                    }
                }
            }
        }

        private void movInfoComplete(object o, ImdbMovieSearchCompletedEventArgs e)
        {
            this.movies = e.Movies;
            //this.lstMovies.Items.Clear();
            foreach (TmdbMovie movie in e.Movies)
            {
                lstMovies.Items.Add(movie.Name);
            }

            this.lblStatus.Text = "Fertig";
        }

        private void lstMovies_DoubleClick(object sender, EventArgs e)
        {
            int c = ((ListBox)sender).SelectedIndex;

            this.lstImg.Items.Clear();

            foreach (TmdbImage img in this.movies[c].Images)
            {
                WebRequest requestPic = WebRequest.Create(img.Url);
                WebResponse responsePic = requestPic.GetResponse();
                Image imgTemp = Image.FromStream(responsePic.GetResponseStream());
                this.imgList.Add(imgTemp);
                this.lstImg.Items.Add(imgTemp);
            }
        }

        private void lstImg_DoubleClick(object sender, EventArgs e)
        {
            Image img = (Image)((ListBox)sender).SelectedItem;
            Rectangle newRect = GetScaledRectangle(img, pictureBox1.ClientRectangle);
            this.pictureBox1.Image = GetResizedImage(img, newRect);
        }

        public static Rectangle GetScaledRectangle(Image img, Rectangle thumbRect)
        {
            if (img.Width < thumbRect.Width && img.Height < thumbRect.Height)
                return new Rectangle(thumbRect.X + ((thumbRect.Width - img.Width) / 2), thumbRect.Y + ((thumbRect.Height - img.Height) / 2), img.Width, img.Height);

            int sourceWidth = img.Width;
            int sourceHeight = img.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)thumbRect.Width / (float)sourceWidth);
            nPercentH = ((float)thumbRect.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            if (destWidth.Equals(0))
                destWidth = 1;
            if (destHeight.Equals(0))
                destHeight = 1;

            Rectangle retRect = new Rectangle(thumbRect.X, thumbRect.Y, destWidth, destHeight);

            if (retRect.Height < thumbRect.Height)
                retRect.Y = retRect.Y + Convert.ToInt32(((float)thumbRect.Height - (float)retRect.Height) / (float)2);

            if (retRect.Width < thumbRect.Width)
                retRect.X = retRect.X + Convert.ToInt32(((float)thumbRect.Width - (float)retRect.Width) / (float)2);

            return retRect;
        }

        public static Image GetResizedImage(Image img, Rectangle rect)
        {
            Bitmap b = new Bitmap(rect.Width, rect.Height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(img, 0, 0, rect.Width, rect.Height);
            g.Dispose();

            try
            {
                return (Image)b.Clone();
            }
            finally
            {
                b.Dispose();
                b = null;
                g = null;
            }
        }

        private void txtMovie_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tmdb.MovieSearchAsync(((TextBox)sender).Text);
                this.lblStatus.Text = "Suche";
            }
        }



    }
}
